package com.wsh.controller.generator;

import com.wsh.base.BaseVo;
import com.wsh.entity.generator.GenBaseConfEntity;
import com.wsh.entity.generator.TableColumnInfoEntity;
import com.wsh.generator.GenBaseConfService;
import com.wsh.generator.TableColumnInfoService;
import com.wsh.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wangsh
* @date 2020-02-16
*/
@Api(tags = "代码生成基本配置管理")
@RestController
@RequestMapping("/utils/genBaseConf")
public class GenBaseConfController {

    @Autowired
    private GenBaseConfService genBaseConfService;

    @Autowired
    private TableColumnInfoService tableColumnInfoService;

    @GetMapping("/list")
    @PreAuthorize("@el.check('genBaseConf:list')")
    @ApiOperation(value = "代码生成基本配置列表")
    public Object list(@RequestParam Map<String, Object> params) {
        return JsonResult.success(genBaseConfService.getList(params));
    }

    @GetMapping("/getById")
    @ApiOperation(value = "查询代码生成基本配置")
    public Object getById(@RequestParam String formId) {
        Map<String, Object> conf = new HashMap<>();
        // 基本配置
        GenBaseConfEntity form = genBaseConfService.getById(formId);
        conf.put("form", form);
        // 查看下拉框keys
        List<TableColumnInfoEntity> tableKeys = tableColumnInfoService.columnList(formId);
        conf.put("tableKeys", tableKeys);

        return JsonResult.success(conf);
    }

    @PostMapping("/save")
    @PreAuthorize("@el.check('genBaseConf:save')")
    @ApiOperation(value = "保存代码生成基本配置")
    public Object saveRole(@RequestBody GenBaseConfEntity classForm) {
        return JsonResult.success(genBaseConfService.saveOrUpdate(classForm));
    }

    @PostMapping("/del")
    @PreAuthorize("@el.check('genBaseConf:del')")
    @ApiOperation(value = "删除代码生成基本配置")
    public Object saveRole(@RequestParam String formId) {
        return JsonResult.success(genBaseConfService.removeById(formId));
    }

}
